package com.example.demo.practice.enterprisemanager;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.demo.practice.R;
import com.example.demo.practice.innerclass.IBaseActivity;
import com.example.demo.practice.tool.ServerHttp;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BreakList extends IBaseActivity {
    private String next = "";
    private String select;
    private String title;

    private List<Map<String, Object>> groupArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_alarm_list);
        Intent intent = getIntent();
        next = intent.getStringExtra("next");
        select = intent.getStringExtra("select");
        title = intent.getStringExtra("title");
        setTitle(title);
        serverHttp =new ServerHttp(new DateHandler());
        serverHttp.setUrl("getFacilities");
    }

    class DateHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            JSONArray jsonArray=serverHttp.getData();
            groupArray = new ArrayList<Map<String, Object>>();
            for (int i = 0; i < jsonArray.length(); i++) {
                Map<String, Object> map = new HashMap<String, Object>();
                JSONObject json = null;
                try {
                    json = (JSONObject) jsonArray.get(i);
                    if(json.getString("status").equals(select)){
                        map.put("code", json.getString("eqindex"));
                        map.put("type", json.getString("name"));
                        map.put("address", json.getString("address"));
                        map.put("geom", json.getString("geom"));
                        map.put("floor", json.getString("floor")+"层");
                        String date = json.getString("ruindate");
                        String[] AuditDates = date.split("T");
                        String time = AuditDates[0];
                        map.put("time","时间：" + time);
                        groupArray.add(map);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            jsonArray = null;
            serverHttp.clear();
            //适配列表
            SimpleAdapter simpleAdapter = new SimpleAdapter(BreakList.this,
                    groupArray,
                    R.layout.etp_item,
                    new String[]{"code", "type", "address", "floor","time"},
                    new int[]{R.id.code, R.id.company, R.id.address, R.id.name,R.id.time});
            ListView listView= (ListView) findViewById(R.id.listView);
            listView.setDivider(null);
            listView.setAdapter(simpleAdapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    try {
                        Intent intent = new Intent(BreakList.this, Class.forName(next));
                        startActivity(intent);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            });
            super.handleMessage(msg);
        }
    }
}
